
import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local/base/BaseItemPage.dart';
import 'package:path_provider/path_provider.dart';

class ImagePage extends BaseItemPage{

  @override
  List<Widget> getItems() {
    var photo =
        "https://up.enterdesk.com/edpic/45/f0/71/45f07166bddcd586f364b249fcd802c0.jpg";
    var assetsPhoto = const AssetImage("images/1111.jpg");
    return <Widget>[
      addItemWidget("图片加载(本地)", () {
        showMyDialog(Image(image: assetsPhoto));
      }),
      addItemWidget("图片加载(文件)", () async {
        await copyAndShowFile("images/1111.jpg");
      }),
      addItemWidget("图片加载(网络)", () {
        //web端网络图片涉及跨域问题
        showMyDialog(Image(image: NetworkImage(photo)));
      }),
    ];
  }

  Future<void> copyAndShowFile(String assetsPhotoFilePath) async {
    getApplicationSupportDirectory().then((value) async {
      log("文件路径:${value.path}");
      var photoFile = File("${value.path}/aaa.jpg");
      var value2 = await photoFile.exists();
      if (value2) {
        log("${photoFile.path},${await photoFile.exists()}");
      } else {
        log("文件不存在，创建并写入内容");
        var bytes = await rootBundle.load(assetsPhotoFilePath);
        await photoFile.create();
        photoFile.writeAsBytes(bytes.buffer.asInt8List(0));
        log("文件复制完毕:${photoFile.path}");
      }
      showMyDialog(Image(image: FileImage(photoFile)));
    });
  }

}